﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using MyLib;

namespace BudgetSet
{
    public partial class NewBudget : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 保存数据至数据库
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckData())
                {
                    string Sql = GetInsertSQL();
                    DataAccess.Instance("BizDB").ExecuteDataTable(Sql);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('新增成功！');", true);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "closeWin", "<script>closeWin();</script>");
                }
            }
            catch (Exception ee)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('新增失败：" + ee.Message + "');", true);
            }
            finally
            {
            }
        }

        /// <summary>
        /// 数据判断
        /// </summary>
        protected Boolean CheckData()
        {
            if (fld_STARTTIME.Text == "")
            {
                Response.Write("<script>alert('预算开始时间不能为空！')</script> ");
                return false;
            }
            else if (fld_ENDTIME.Text == "")
            {
                Response.Write("<script>alert('预算结束时间不能为空！')</script> ");
                return false;
            }
            else if (hf_BudgetType.Value == "")
            {
                Response.Write("<script>alert('预算类型不能为空！')</script> ");
                return false;
            }
            else if (fld_BUDGETCODE.Text == "")
            {
                Response.Write("<script>alert('预算编号不能为空！')</script> ");
                return false;
            } 
            else if (fld_DEPARTMENT.Text == "")
            {
                Response.Write("<script>alert('部门不能为空！')</script> ");
                return false;
            }
            else if (fld_ITEMSCODE.Text == "")
            {
                Response.Write("<script>alert('项目代码不能为空！')</script> ");
                return false;
            }
            else if (fld_ITEMSDESC.Text == "")
            {
                Response.Write("<script>alert('项目描述不能为空！')</script> ");
                return false;
            }
            else if (fld_COSTCENTER.Text == "")
            {
                Response.Write("<script>alert('成本中心不能为空！')</script> ");
                return false;
            }
            else if (fld_COSTCENTERNAME.Text == "")
            {
                Response.Write("<script>alert('成本中心名称不能为空！')</script> ");
                return false;
            }
            else if (fld_AMOUNT.Text == "" || fld_AMOUNT.Text == "0")
            {
                Response.Write("<script>alert('预算金额不能为0！')</script> ");
                return false;
            }
            else if (fld_BUDGETCODE.Text != "")
            {
                //当BudgetCode与数据库里存在时，对用户进行提示
                string SqlRecord = string.Format("select * from PROC_BUDGETSET where BUDGETCODE ='{0}' ", fld_BUDGETCODE.Text);
                DataTable dtRecord = DataAccess.Instance("BizDB").ExecuteDataTable(SqlRecord);
                if (dtRecord.Rows.Count > 0)
                {
                    Response.Write("<script>alert('预算编号为：" + fld_BUDGETCODE.Text + "的记录已经存在！')</script> ");
                    return false;
                }
                else return true;
            }
            else
                return true;
        }

        /// <summary>
        /// 拼接InsertSQL
        /// </summary>
        protected string GetInsertSQL()
        {
            string StrFields = "DEPARTMENT,STARTTIME,ENDTIME,BUDGETTYPE,BUDGETCODE,ITEMSCODE,ITEMSDESC,COSTCENTER,COSTCENTERNAME,COSTCATEGORY1,COSTCATEGORY2,ACCOUNTCODE,"
                + "ACCOUNTDESC,DETAILACCOUNTCODE,DETAILACCOUNTDESC,REASONS,DESCRIPTION,OWNER,LIFE,QTY,AMOUNT,CAPEXBALANCE,PRBALANCE,POBALANCE,DH,RECLASS";

            string StrValues = "'" + fld_DEPARTMENT.Text
                             + "' ,'" + fld_STARTTIME.Text
                             + "' ,'" + fld_ENDTIME.Text
                             + "' ,'" + hf_BudgetType.Value
                             + "' ,'" + fld_BUDGETCODE.Text
                             + "' ,'" + fld_ITEMSCODE.Text
                             + "' ,'" + fld_ITEMSDESC.Text
                             + "' ,'" + fld_COSTCENTER.Text
                             + "' ,'" + fld_COSTCENTERNAME.Text
                             + "' ,'" + fld_COSTCATEGORY1.Text
                             + "' ,'" + fld_COSTCATEGORY2.Text
                             + "' ,'" + fld_ACCOUNTCODE.Text
                             + "' ,'" + fld_ACCOUNTDESC.Text
                             + "' ,'" + fld_DETAILACCOUNTCODE.Text
                             + "' ,'" + fld_DETAILACCOUNTDESC.Text
                             + "' ,'" + fld_REASONS.Text
                             + "' ,'" + fld_DESCRIPTION.Text
                             + "' ,'" + fld_OWNER.Text
                             + "' ," + fld_LIFE.Text
                             + " ," + fld_QTY.Text
                             + " ," + fld_AMOUNT.Text
                             + " ," + fld_CAPEXBALANCE.Text
                             + " ," + fld_PRBALANCE.Text
                             + " ," + fld_POBALANCE.Text
                             + " ,'" + fld_DH.Text
                             + "' ,'" + fld_RECLASS.Text
                             + "'";
            string Sql = string.Format("insert into PROC_BUDGETSET ({0}) values({1})", StrFields, StrValues);
            return Sql;
        }
    }
}